﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuaInterface;
using UnityEngine;

namespace GMBase.Lua
{
    /// <summary>
    /// lua静态加载。
    /// </summary>
    class LuaLoaderForResource : LuaFileUtils
    {
        public const string LuaRootPath = "Lua/";

        public LuaLoaderForResource()
        {
            LuaFileUtils.Instance = this;
        }
        /// <summary>
        /// 当LuaVM加载Lua文件的时候，这里就会被调用，
        /// 用户可以自定义加载行为，只要返回byte[]即可。
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public override byte[] ReadFile(string fileName)
        {
            return ReadFileFromResource(fileName);
        }

        /// <summary>
        /// 方便外部使用
        /// </summary>
        /// <returns></returns>
        public static byte[] ReadFileFromResource(string fileName)
        {
            if (!fileName.EndsWith(LuaFileMgr.LuaExternName))
            {
                fileName += LuaFileMgr.LuaExternName;
            }
            var pathFromRes = LuaRootPath + fileName;

            TextAsset luaAsset = Resources.Load(pathFromRes) as TextAsset;

            return luaAsset.bytes;
        }
    }
}
